package com.qiantu.phone.ui.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.entity.ApplyTransferBean;
import com.qiantu.api.entity.MessageBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.action.StatusAction;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.NoticeAdapter;
import com.qiantu.phone.ui.dialog.ConfirmHintDialog;
import com.qiantu.phone.widget.StatusLayout;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class NoticeListActivity extends AppActivity implements OnRefreshLoadMoreListener, StatusAction {

    private StatusLayout mStatusLayout;
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout smartRefreshLayout;
    private NoticeAdapter mNoticeAdapter;
    private int pageIndex = 1;
    private int pageSize = 10;
    //1初始化  2刷新  3加载更多
    private int requestType = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notice;
    }

    @Override
    protected void initView() {
        mStatusLayout = findViewById(R.id.status_layout);
        smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mNoticeAdapter = new NoticeAdapter(this);
        mNoticeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                MessageBean.ListDTO message = mNoticeAdapter.getItem(position);
                if(message.getSubCategory() == 3101){
                    getTransferDetail(message.getSource());
                }
            }
        });
        mRecyclerView.setAdapter(mNoticeAdapter);
        smartRefreshLayout.setOnRefreshLoadMoreListener(this);
        showLoading();
    }

    private void getTransferDetail(String transferSerialNo){
        LLHttpManager.getTransferDetail(this, transferSerialNo, new HttpCallback<HttpData<ApplyTransferBean>>(null){
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<ApplyTransferBean> result) {
                ApplyTransferBean applyTransferBean = result.getData();
                if(applyTransferBean != null){
                    // 1:待交付 2:已交付 3:已拒绝 4:已撤回
                    switch (applyTransferBean.getState()){
                        case 1:
                            AppApplication.getInstance().showTransferDialog(NoticeListActivity.this, applyTransferBean.getContent(), applyTransferBean.getTransferSerialNo(), applyTransferBean.getHouseSerialNo(), applyTransferBean.getCongratulations());
                            break;
                        case 2:
                            showHintDialog(R.string.transfer_received);
                            break;
                        case 3:
                            showHintDialog(R.string.transfer_rejected);
                            break;
                        case 4:
                            showHintDialog(R.string.transfer_canceled);
                            break;
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show("获取过户信息失败");
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private BaseDialog mConfirmHintDialog;
    private ConfirmHintDialog.Builder mConfirmHintDialogBuilder;
    private void showHintDialog(int res){
        if (mConfirmHintDialog == null) {
            mConfirmHintDialogBuilder = new ConfirmHintDialog.Builder(this)
                    .setRightBtnText(getString(R.string.i_got_it))
                    .setListener(new ConfirmHintDialog.OnListener() {
                        @Override
                        public void onLeftClick(BaseDialog dialog) {
                        }

                        @Override
                        public void onConfirm(BaseDialog dialog) {

                        }
                    });
            mConfirmHintDialog = mConfirmHintDialogBuilder.create();
        }
        mConfirmHintDialogBuilder.setContent(res);
        mConfirmHintDialog.show();
    }

    @Override
    protected void initData() {
        //获取通知消息
        LLHttpManager.getMessages(this, pageIndex, pageSize, 3,AppApplication.getInstance().getCurrentHouseSerialNo(), new HttpCallback<HttpData<MessageBean>>(this) {
            @Override
            public void onSucceed(HttpData<MessageBean> result) {
                super.onSucceed(result);
                if (result != null && result.getData() != null) {
                    switch (requestType){
                        case 1:
                            mNoticeAdapter.setData(result.getData().getList());
                            break;
                        case 2:
                            mNoticeAdapter.clearData();
                            mNoticeAdapter.setData(result.getData().getList());
                            break;
                        case 3:
                            result.getData().getList();
                            mNoticeAdapter.addData(result.getData().getList());
                            mNoticeAdapter.setLastPage(result.getData().getList() == null || result.getData().getList().size() < 10);
                            break;
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
                switch (requestType){
                    case 1:
                        showComplete();
                        break;
                    case 2:
                        smartRefreshLayout.finishRefresh();
                        break;
                    case 3:
                        smartRefreshLayout.finishLoadMore();
                        smartRefreshLayout.setNoMoreData(mNoticeAdapter.isLastPage());
                        break;
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        requestType = 2;
        pageIndex = 1;
        initData();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        requestType = 3;
        pageIndex = (int) Math.ceil(((float) mNoticeAdapter.getData().size() / pageSize)) + 1;
        initData();
    }

    @Override
    public StatusLayout getStatusLayout() {
        return mStatusLayout;
    }
}